<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../lib/axios.js"></script>
</head>
<body>
    <h1>新增地址</h1>
    用户名: <input type="text" class="user"><br>
    手机号: <input type="text" class="phone"><br>
    收货地址: <input type="text" class="dz"><br>
    <button>保存</button>
    <script>
        let user=document.querySelector('.user');
        let phone=document.querySelector('.phone');
        let dz=document.querySelector('.dz');
        let btn=document.querySelector('button')
        
        // 设置手机号正则
        let get=/^1[3-9]\d{9}$/;

        // 给保存按钮添加点击事件
        btn.onclick=function(){
            // 判断输入内容长度
            if(user.value.trim().length<=0||phone.value.trim().length<=0||dz.value.trim().length<=0){
                alert('输入内容格式错误')
                return
            }
            // 判断手机号输入是否正确
            if(!get.test(phone.value.trim())){
                alert('输入手机号不正确')
                return
            } 

            axios.get('/new ',{
                name:user.value.trim(),
                phont:phone.value.trim(),
                dz:dz.value.trim()
            }).then((res) => {
                if(res.data.code===1){
                    location.href='/'
                }
            })
        }
    </script>
</body>
</html>